<?php

// $Id$

global $WSC;

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_cia.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_value_dbc.class.php');


class TCMS_CIA_Nomenclature_DBC extends TCMS_CIA implements I_DB_Storable {
	public function is_valid_value($value, DB_Connection $dbc = NULL, $nomenclature = '') {
		$ret = false;

		if ($dbc && $nomenclature) {
			$nomenclature = TCMS_Nomenclature_DBC();
			if (!$nomenclature->load_by_hid($dbc, $nomenclature_hid)) {
				throw new Tangra_Module_Exception('Cannot find nomenclature with hid = $nomenclature_hid');
			}


			$ret = TCMS_Nomenclature_Value_DBC::exists_for_nomenclature($dbc, $nomenclature->get_id(), $value);

		} elseif(tangra_is_int($value) && $value > 0) {
			$ret = true;
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select ci_type_attr, ".
						"ci, ".
						"value ".
					"from tcms_cia_nomenclature where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_ci_type_attr($rez_obj->CI_TYPE_ATTR);
			$this->set_ci($rez_obj->CI);
			$this->set_value($rez_obj->VALUE);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_cia_nomenclature_seq');
		$ci_type_attr = $this->get_ci_type_attr();
		$ci = $this->get_ci();
		$value = $this->get_value();

		if ($id) {
			$sql = "insert into tcms_cia_nomenclature (id, ".
									      "ci_type_attr, ".
									      "ci, ".
									      "value ".
									      ") ".
								"values ".
									      "($id,".
									      "$ci_type_attr, ".
									      "$ci, ".
									      "$value ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_cia_nomenclature_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$ci_type_attr = $this->get_ci_type_attr();
		$ci = $this->get_ci();
		$value = $this->get_value();

		$sql = "update tcms_cia_nomenclature set ".
						"ci_type_attr = $ci_type_attr, ".
						"ci = $ci, ".
						"value = $value ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public function load_by_ci_thid(DB_Connection $dbc, $ci_id, $attr_type_hid) {
		tangra_if_not_int_throw_e($ci_id);
		$attr_type_hid = addslashes($attr_type_hid);

		$ret = false;
		$sql = "select cia_nom.id ".
					"from tcms_cia_nomenclature cia_nom, ".
							"tcms_ci_type_attrs cita ".
					"where cia_nom.ci = $ci_id and ".
							"cia_nom.ci_type_attr = cita.id and ".
							"cita.hid = '$attr_type_hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function load_all_by_ci_tid(DB_Connection $dbc, $ci_id, $attr_type_id) {
		tangra_if_not_int_throw_e($ci_id);
		$attr_type_id = $attr_type_id;

		$sql = "select cia_nom.id ".
					"from tcms_cia_nomenclature cia_nom ".
					"where cia_nom.ci = $ci_id and ".
							"cia_nom.ci_type_attr = $attr_type_id ";


		$rez = $dbc->execute($sql);
		$ret = array();
		if ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_CIA_Nomenclature_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);

			$ret[] = clone $tmp;
		}

		return $ret;
	}


	public static function select_all_by_ci_tid_nom(DB_Connection $dbc, $ci_id, $attr_type_id, $nom_id) {
		tangra_if_not_int_throw_e($ci_id);
		$attr_type_id = $attr_type_id;

		$sql = "select nom_vals.value ".
					"from tcms_cia_nomenclature cia_nom, ".
							"tcms_nomenclature_values nom_vals ".
					"where cia_nom.ci = $ci_id and ".
							"cia_nom.ci_type_attr =  $attr_type_id and ".
							"nom_vals.nomenclature = $nom_id and ".
							"cia_nom.value = nom_vals.id";

		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = $rez_obj->VALUE;
		}

		return $ret;
	}


	public function get_valid_values(DB_Connection $dbc = NULL, $nomenclature_hid = '') {
		$ret = array();

		if (!$dbc || !$nomenclature) {
			throw new Tangra_Module_Exception('$dbc or $nomenclature is not set.');
		}

		$nomenclature = TCMS_Nomenclature_DBC();
		if (!$nomenclature->load_by_hid($dbc, $nomenclature_hid)) {
			throw new Tangra_Module_Exception('Cannot find nomenclature with hid = $nomenclature_hid');
		}


		$items = TCMS_Nomenclature_Value_DBC::load_all_for_nomenclature($dbc, $nomenclature->get_id());
		foreach($items as $item) {
			$ret[] = $item->get_id();
		}


		return $ret;
	}


	public function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);
		$sql = "delete from tcms_cia_nomenclature where id = $id";
		$rez = $dbc->execute($sql);
	}


	public function get_sql_select_id_ci_for_ci_type_attr($ci_type_attr) {
		tangra_if_not_int_throw_e($ci_type_attr);
		$sql = "select id, ci from tcms_cia_nomenclature where ci_type_attr = $ci_type_attr";
		return $sql;
	}


	public function count_all_for_ci_type_attr(DB_Connection $dbc, $ci_type_attr) {
		$sql = "select count(id) as c from tcms_cia_nomenclature where ci_type_attr =  $ci_type_attr";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public function delete_all_for_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);
		$sql = "delete from tcms_cia_nomenclature where ci = $ci_id";
		$rez = $dbc->execute($sql);
	}


	public static function count_all_for_nom(DB_Connection $dbc, $ciat_id, $nom_hid, $nom_value_id) {
		tangra_if_not_int_throw_e($ciat_id);
		$nom_hid = addslashes($nom_hid);
		tangra_if_not_int_throw_e($nom_value_id);

		$sql = "select count(cia_nom.id) as c ".
					"from tcms_cia_nomenclature cia_nom, ".
							"tcms_ci_type_attrs cita ".
					"where cita.attr_type = $ciat_id and ".
							"cita.params = '$nom_hid' and ".
							"cita.id = cia_nom.ci_type_attr and ".
							"cia_nom.value = $nom_value_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}
}

